<?php

namespace Admin\Controller;

use Think\Controller;

class CategoryController extends Controller
{
    public function lst()
    {
        $model = D('category');
        $data = $model->getTree();


        $this->assign(array(
            'data' => $data,
            '_page_title' => '分类列表',
            '_page_btn_name' => '添加新分类',
            '_page_btn_link' => U('add'),
        ));
        $this->display();
    }

    public function add()
    {
        $model = D('Category');
        if(IS_POST)
        {
            if($model->create(I('post.'),1))
            {
                if($id = $model->add())
                {
                    $this->success('添加成功！',U('lst?p='.I('get.p')));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $catData = $model->getTree();
        $this->assign(array(
            'catData' => $catData,
            '_page_title' => '添加新分类',
            '_page_btn_name' => '分类列表',
            '_page_btn_link' => U('lst'),
        ));
        $this->display();
    }

    public function edit()
    {
        $model = D('Category');
        if(IS_POST)
        {
            if($model->create(I('post.'),2))
            {
                if($id = $model->add())
                {
                    $this->success();
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $data = $model->find($id);
        $catData = $model->getTree();
        $children = $model->getChildren($id);
        $this->assign(array(
            'children' => $children,
            'data' => $data,
            'catData' => $catData,
        ));
        $this->assign(array(
            '_page_title' => '修改分类',
            '_page_btn_name' => '分类列表',
            '_page_btn_link' => U('lst'),
        ));
    }
}